package cn.itcast.querySolr;


import cn.itcast.solrj.cn.itcast.solrj.pojo.Student;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.junit.Test;

import java.util.List;

public class TestQuerySolr {
    @Test
    public void SolrIndexQuery() throws SolrServerException {
        //连接服务
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");

        //创建查询查询对象
        SolrQuery solrQuery = new SolrQuery("title:手机");

        //执行solr的查询请求 获取结果
        QueryResponse queryResponse = solrServer.query(solrQuery);

        //从响应结果数据中解析结果
        List<Student> beans = queryResponse.getBeans(Student.class);
        for (Student bean : beans) {
            String id = bean.getId();
            Object title = bean.getTitle();
            Float price = Float.valueOf(bean.getPrice());
            System.out.println("id:"+id+"title:"+title+"price:"+price);
        }


    }
}
